const Sequelize = require("sequelize");
const dbScopes = require("./db-scopes");
console.log("\n\n\n", process.env.DB_NAME);
const sequelize = new Sequelize(
  process.env.DB_NAME,
  process.env.DB_USER,
  process.env.DB_PASSWORD,
  // "bosox",
  // "root",
  // "1234",
  {
    dialect: "mysql",
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    // host: "localhost",
    // port: 3306,
    logging: false,
    timezone: "Asia/Shanghai", // 正确的时区配置
    define: {
      timestamps: true,
      // delete_time
      paranoid: true,
      createdAt: "created_at",
      updatedAt: "updated_at",
      deletedAt: "deleted_at",
      // 把驼峰命名转换为下划线
      underscored: true,
      scopes: dbScopes,
    },
  }
);

// 创建模型
sequelize.sync({ force: false });

sequelize
  .authenticate()
  .then((res) => {
    console.log("``````\nConnection has been established successfully.");
  })
  .catch((err) => {
    console.error("Unable to connect to the database:", err);
  });

// sequelize
//   .query(
//     "CREATE DATABASE IF NOT EXISTS boblog DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci"
//   )
//   .then((res) => {
//     console.log("CREATE DATABASE SUCCESS!");
//   })
//   .catch((err) => {
//     console.log("CREATE DATABASE FAIL!", err);
//   });

module.exports = {
  sequelize,
};
